/*
9
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
3
A 2 B 10 C 40
B 1 C 20
0

216
30
*/
#include <cstdio>
#include <climits>
#include <cstring>
using namespace std;

int path[26][2];
int mymap[26][26];

void GoFrom(int start, int n, int& mini)
{
    for(int i=0; i<n; i++)
    {
        if(path[i][0]==0&&mymap[start][i]!=0)
        {
            if (mymap[start][i] < path[i][1])
            path[i][1]= mymap[start][i];
        }
    }

    int mvili=-1, mwei;
    for(int i=0; i<n; i++) if (path[i][0]==0&&path[i][1]!=INT_MAX){mvili = i; mwei = path[i][1];break;}
    if(mvili == -1) return;

    for(int i=mvili+1; i<n; i++)
    {
        if (path[i][0]==0&&path[i][1]<mwei) {mvili = i; mwei = path[i][1];}
    }
    mini += mwei;
    path[mvili][0] = 1;
    GoFrom(mvili, n, mini);
}

int main()
{
    int output[101], times = 0;
    while(1)
    {
        int mini = 0;
        int n;scanf("%d", &n);if(n==0) break;
        ++times;
        memset(mymap, 0, sizeof(mymap));
        for (int i=0; i<n; i++) {path[i][0]=0; path[i][1]=INT_MAX;}

        for(int i=0; i<n-1; i++)
        {
            getchar();
            char labl; int road;
            scanf("%c %d", &labl, &road);

            for(; road>0; --road)
            {
                char endv; int vili,wei;
                scanf(" %c %d",&endv,&wei);
                vili = (int)endv-65;
                mymap[i][vili] = wei;
                mymap[vili][i] = wei;
            }
        }

        path[0][0]=1;path[0][1]=0;
        GoFrom(0, n, mini);
        output[times] = mini;
    }
    for(int i=1; i<=times; i++) printf("%d\n", output[i]);
    return 0;
}
